package com.fakeDYChat.common.user.dao;

import com.fakeDYChat.common.common.domain.enums.YesOrNoEnum;
import com.fakeDYChat.common.user.domain.entity.User;
import com.fakeDYChat.common.user.mapper.UserMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 用户表 服务实现类
 * </p>
 *
 * @author Tang
 * @since 2023-11-18
 */
@Service
public class UserDao extends ServiceImpl<UserMapper, User> {

    /**
     * 通过用户的openid查找用户信息
     *
     * @param openId
     * @return com.fakeDYChat.common.user.domain.entity.User
     * @author Tang
     * @date 2023/12/30 19:51
     **/
    public User getUserOneByOpenId(String openId) {
        return lambdaQuery().eq(User::getOpenId, openId).one();
    }

    /**
     * 更新用户信息
     *
     * @param user
     * @return void
     * @author Tang
     * @date 2023/12/30 19:52
     **/
    public void updateUserInfo(User user) {
        lambdaUpdate()
                .eq(User::getOpenId, user.getOpenId())
                .set(User::getName, user.getName())
                .set(User::getAvatar, user.getAvatar())
                .update();
    }

    /**
     * 根据用户昵称查找用户
     *
     * @param newName
     * @return com.fakeDYChat.common.user.domain.entity.User
     * @author Tang
     * @date 2023/12/30 19:52
     **/
    public User getByName(String newName) {
        return lambdaQuery()
                .eq(User::getName, newName)
                .one();
    }

    /**
     * 根据用户id修改用户昵称
     *
     * @param uid     用户id
     * @param newName 用户昵称
     * @author Tang
     * @date 2023/12/30 20:33
     **/
    public void modifyName(Long uid, String newName) {
        lambdaUpdate()
                .eq(User::getId, uid)
                .set(User::getName, newName)
                .update();
    }

    /**
     * 更新用户佩戴的徽章
     *
     * @param uid    用户id
     * @param itemId 物品id
     * @author Tang
     * @date 2024/1/2 20:03
     **/
    public void wearingBadge(Long uid, Long itemId) {
        lambdaUpdate()
                .eq(User::getId, uid)
                .set(User::getItemId, itemId)
                .update();
    }

    /**
     * 查询用户是第几个注册
     *
     * @param id
     * @return java.lang.Integer
     * @author Tang
     * @date 2024/1/4 18:36
     **/
    public Integer getIthRegister(Long id) {
        return lambdaQuery()
                .le(User::getId, id)
                .count();
    }

    /**
     * 根据uid将用户更新为拉黑状态
     *
     * @param uid 用户id
     * @author Tang
     * @date 2024/1/12 21:11
     **/
    public void blackUserByUid(Long uid) {
        lambdaUpdate().eq(User::getId, uid)
                .set(User::getStatus, YesOrNoEnum.YES.getStatus())
                .update();
    }
}
